<template>
  <div class="data-list" v-on="$listeners">
    <d2-crud
      ref="d2Crud"
      :data="dataList"
      :columns="columns"
      :options="options"
      :loading="loading"
      :pagination="pagination"
      selection-row
      @selection-change="selectChange"
      @pagination-current-change="pageChange"
      @current-change="clickRow"
    >
    </d2-crud>
  </div>

</template>
<script>
  import deptApi from "@/api/system/dept";
  import roleApi from "@/api/system/role";
  import caseApi from "@/api/system/case";
  import utils from '@/assets/utils/zhzf-util';

  export default {
    name: "list-table",
    data() {
      return {
        loading: false,
        dataList: [],
        columns: [],
        selected: [],
        pagination: {
          currentPage: 1,
          pageSize: utils.getPageSize(),
          total: 0,
        },
        options: {
          stripe: true,
          highlightCurrentRow: true,
          height: utils.getPageheight(),
        },
        query: {},
        pageParam: {},
        isSignal: false,
      };
    },
    methods: {
      async initPage({method, cols, param, isSignal, noPage}) {
        this.query = param || {};
        cols.unshift({
          title: '序号', key: 'no', minWidth: 40, formatter: (row, col, val, index) => {
            let flag = (this.pagination.currentPage - 1) * this.pagination.pageSize;
            return flag + index + 1;
          }
        })
        this.pageParam = {method, cols, param, isSignal, noPage};
        await this._fetchData();
      },
      async reloadData(param) {
        this.query = param;
        await this._fetchData();
      },
      async _fetchData() {
        this.loading = true;
        this.columns = this.pageParam.cols;
        let res = await this.pageParam.method({
          ...this.query,
          ...(this.pageParam.noPage ? {} : this.pagination)
        });
        if (this.pageParam.noPage) {
          this.dataList = res.data;
          this.pagination.total = this.dataList.length;
        } else {
          this.dataList = res.data.rows||res.data;
          this.pagination.total = res.data.total;
        }
        this.loading = false;
      },
      selectChange(selection) {
        this.$emit('selected', selection);
      },
      async pageChange(page) {
        this.pagination.currentPage = page;
        await this._fetchData();
      },
      clickRow(row) {
        //this.$refs.d2Crud.$children[0].clearSelection();
        this.currentrow = row
        if (!this.isSignal) {
          let els = this.$refs.d2Crud.$children;
          this.$refs.d2Crud.$children[els.length == 1 ? 0 : els.length - 2].toggleRowSelection(row);
          // this.$refs.d2Crud.$children[0].toggleRowSelection(row);
        }
      }
    }
  };
</script>
<style scoped lang="scss">
  .el-dialog__body {
    padding-top: 0 !important;

    .data-list {
      padding: 0;

      .el-select--default {
        width: 60% !important;
      }

      .d2-crud {
        max-height: 55vh;
        overflow: auto;
      }
    }

  }
</style>
